<template>
  <div class="articleList">
    <Loading v-if="total == null"></Loading>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索文章区域 -->
      <el-row>
        <el-col :span="8"
          ><el-input
            placeholder="请输入标题关键字查询"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getArticleList"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getArticleList"
              ></el-button>
            </template> </el-input
        ></el-col>
      </el-row>

      <el-table :data="articleList" border stripe class="mt10">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column
          prop="title"
          label="文章标题"
          align="left"
          header-align="center"
        >
        </el-table-column>
        <el-table-column label="权重" width="50px" prop="weight" align="center">
        </el-table-column>
        <el-table-column label="创建时间" width="190px" align="center">
          <template #default="scope">
            {{ formatTime(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              @click="router.push(`/articles/edit/${scope.row._id}`)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="removeArticle(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getCurrentInstance,
  onBeforeMount,
  reactive,
  toRefs,
} from "@vue/runtime-core";
import { useRouter } from "vue-router";
import { getData, deleteData } from "../../services/http";
import { formatTime } from "../../libs/utils";
export default {
  name: "articleList",
  setup() {
    const { proxy } = getCurrentInstance();
    const router = useRouter();
    const state = reactive({
      articleList: [],
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: null,
    });
    const stateAsRefs = toRefs(state);
    const getArticleList = async () => {
      const { data: res } = await getData("rest/articles", {
        params: state.queryInfo,
      });
      state.articleList = res.data;
      state.total = res.total;
    };

    const removeArticle = (row) => {
      proxy
        .$confirm(`确定要删除 ${row.title}吗？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async () => {
          await deleteData(
            `rest/articles/${row._id}`,
            `已成功删除文章 ${row.title}`
          );
          getArticleList();
        });
    };
    const handleCurrentChange = (newPage) => {
      state.queryInfo.pageNum = newPage;
      getArticleList();
    };
    const handleSizeChange = (newSize) => {
      state.queryInfo.pageSize = newSize;
      getArticleList();
    };
    onBeforeMount(() => {
      getArticleList();
    });
    return {
      ...stateAsRefs,
      router,
      removeArticle,
      formatTime,
      handleCurrentChange,
      handleSizeChange,
      getArticleList,
    };
  },
};
</script>
